<?php
require_once("config.php");
$now = $today = date("YmdHis");
if(isset($_FILES["myfile"])) 
{
	if ($_FILES["myfile"]["error"] > 0)
	{
	  echo "Error: " . $_FILES["file"]["error"] . "<br>";
	}
	else {
		$theFile = $_FILES["myfile"]["name"];
		$recipient = $_POST["recipient"];
		$sender = $_POST["sender"];
		echo "<br>Antivirus Scan In Progress...<br>";
		$command = $myClamscan . " " . $_FILES["myfile"]["tmp_name"];
		$out = "";
		$int = -1;
		exec($command, $out, $int);		
		if($int==0)
		{
			// all good
			// Hash the file so that other files with the same name won't overwrite it. Add the date so that identical files with a different name won't overwrite it.
			echo "File hashing in progress...<br>";
			$fileHash=hash_file("sha256",($_FILES["myfile"]["tmp_name"])) . $now;
			//move the uploaded file to uploads folder;
			move_uploaded_file($_FILES["myfile"]["tmp_name"],$upload_dir . $fileHash);
			file_put_contents($upload_dir . $fileHash . ".name",$theFile);
			echo "Uploaded File: " . $theFile;
			$to = $recipient;
			$subject = $sender . " has sent you a file";
			$message = $sender . " has sent you the file: " . $theFile . ". To download this file, please visit the following web address: \r\nhttp://" . $webPath . "get.php?fileid=" . $fileHash;
			$from = "no-reply@netshadow.org";
			$headers = "From:" . $from;
			if ($_POST["ccme"] == "yes") {
				$headers .= "\r\nCc: " . $sender;
			}
			mail($to,$subject,$message,$headers);
			echo "<br>Mail Sent.";
		} else {
		        // Virus Detected
			echo $theFile ." was not uploaded because it is infected with a virus or other malware.";
			$to = $sender;
			$subject = "Your file was infected";
			$message = "You attempted to send the file: " . $theFile . " to " . $recipient . ". Our anti-virus scanner determined that " . $thefile . " was infected, so it was not sent to " . $recipient . ".";
			$from = "no-reply@netshadow.org";
			$headers = "From:" . $from;
			mail($to,$subject,$message,$headers);
		}
	}
}
?>